<?php
/**  
 * 友情链接管理
 * 对友情链接的增删改查
 * @access public|private|protected
 * @param string xxx
 * @return void|int|string|boolean|array        comment 
 * @author      ZhangLiLi <379417738@qq.com>
 * @version	  v1.0.0 
 * @copyright  2017-1-17 下午2:31:52
*/
namespace Admin\Controller;
use Admin\Controller\AdminController;
class LinkController extends AdminController{
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	/**
	 * 友情链接列表页显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午2:07:36
	 */
	public function index(){
		$where["status"]=array("NEQ","-1");
		$count=M("Link")->where($where)->count();
		$Page = new \Think\Page($count,10);
		$show = $Page->show();
		$list=M('Link')->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	/**
	 * 友情链接添加或修改页的显示
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午2:07:59
	 */
	public function insert(){
		$id=I("id","");
		if(!empty($id)){
			$where["status"]=array("NEQ","-1");
			$where["id"]=$id;
			$list=M("Link")->where($where)->find();
			$this->assign("list",$list);
			$but="修改";
		}else{
			$but="添加";
		}
		$this->assign("but",$but);
		$this->display();
	}
	public function addLink(){
		$where["id"]=I("id","");
		$data["title"]=I("title","");
		$data["url"]=I("url","");
		$data["add_time"]=time();
		if(empty($where["id"])){
			//添加
			if(empty($data["title"])){
				$this->error("请填写标题");
			}
			if(empty($data["url"])){
				$this->error("请填写链接地址");
			}
			$data["status"]=I("status",1);
			$res=M("Link")->add($data);
			if($res){
				$str_su="添加成功";
			}else{
				$str_er="添加失败";
			}
		}else{
			//修改
			if(empty($data["title"])){
				$this->error("请填写标题");
			}
			if(empty($data["url"])){
				$this->error("请填写链接地址");
			}
			$res=M("Link")->where($where)->save($data);
			if($res){
				$str_su="修改成功";
			}else{
				$str_er="修改失败";
			}
		}
		if($res){
			$this->success($str_su,U("Link/index"));
		}else{
			$this->error($str_er);
		}
	}
	/**
	 * 修改状态
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2017-1-17 下午2:25:46
	 */
	public function open(){
    	$where['id']=I('id')?I('id'):null;
    	$list=M('Link')->where($where)->find();
    	if($list['status']==2){
    		$list=M('Link')->where($where)->setField('status','1');
    		$this->success('状态修改成功');
    	}
    	if($list['status']==1){
    		$list=M('Link')->where($where)->setField('status','2');
    		$this->success('状态修改成功');
    	}
    }
    /**
     * 删除 
     * 就是把状态改成了-1 表里还会有数据
     * @author      ZhangLiLi <379417738@qq.com>
     * @version	  v1.0.0 
     * @copyright  2017-1-17 下午2:27:11
     */
	public function del(){
    	$where['id']=I("id","");
		if(!empty($where['id'])){
			$list=M('Link')->where($where)->find();
			if($list["status"]==2){
				$res=M('Link')->where($where)->setField('status','-1');
				if($res){
					$this->success('删除成功',U('Link/index'));
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error("显示状态下不能删除");
			}
		} 
    }
}